﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;
using System.Xml.Linq;
using System.IO;

namespace Orion.Lib
{

    public static class LevelManager
    {

        public static List<LevelDescriptor> LevelList = new List<LevelDescriptor>();

        static LevelManager()
        {
            LoadLevelList();
        }
        //public static LevelManager Instance = new LevelManager();
        private static void LoadLevelList()
        {

            var rundir = new FileInfo(Assembly.GetExecutingAssembly().FullName).Directory;
            foreach (var lib in rundir.GetFiles("*.dll"))
            {
                //looking assamblies
                var assambly = Assembly.LoadFile(lib.FullName);
                //looking for provider
                var types = assambly.GetTypes();
                foreach (var type in types)
	            {
                    if (type.GetInterface("ILevelProvider") != null)
                    {
                        //got it
                        var provider = Activator.CreateInstance(type, MyGame.Instance) as ILevelProvider;
                        LevelList.AddRange(provider.GetLevels(provider));
                    }
	            } 
            }
        }

        public delegate void LevelComplete();
        public delegate void LevelFailed();
        public delegate void LevelLoaded();
        public static void LoadLevel(LevelDescriptor ld, LevelLoaded onLoaded,LevelComplete onComplete, LevelComplete onFailed)
        {
            var lfm = new LevelFlowManager(ld.provider.GetLevelFile(ld));
            var screen = new StageScreen(ld, lfm);
            MyGame.Instance.SwitchScreen(screen);
        }
    }
}
